'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: mcp_crud_handle.py
* @Time: 2025/11/20
* @All Rights Reserve By Brtc
'''
from mcp_core.mcp_client import MCPClient
from mcp_core.mcp_protocol import StatusCode


def example_basic_operation():
    print("基本的MCP  CRUD 操作")
    with MCPClient(host='127.0.0.1', port=8080) as client:
        #1、创建上下文
        print("创建上下文！")
        response = client.create_context(
            content={"user":"Alice", "message":"Hello MCP!"},
            metadata = {"labels":["gretting", "user"], "scope":"session"},
            extensions={"priority":"high"}
        )
        print(f"   Status: {response.status_code}")
        print(f"   Message:{response.message}")
        if response.status_code == StatusCode.CREATED.value:
            context_id = response.data["context"]["id"]
            print(f"Created context: {context_id}")
        else:
            print(f"创建失败！")
            return

        #2、读取上线文
        print("读取上下文！！")
        response = client.read_context(context_id)
        print(f"   Status: {response.status_code}")
        print(f"   Message:{response.message}")
        if response.status_code == StatusCode.SUCCESS.value:
            context = response.data["context"]
            print(f"Context Content : {context}")

        #3、更新上下文
        print("更新上下文！！")
        response = client.update_context(
            context_id,
            content={"user":"Alice", "message":"Hello MCP!", "status":"updated"},
            metadata = {"labels":["gretting", "user", "updated"]},
        )
        print(f"   Status: {response.status_code}")
        print(f"   Message:{response.message}")

        #4、验证更新后上下文
        response = client.read_context(context_id)
        if response.status_code == StatusCode.SUCCESS.value:
            context = response.data["context"]
            print(f"Updated Context Content : {context}")


        #5、删除上下文
        print("删除上下文！")
        response = client.delete_context(context_id)
        print(f"   Status: {response.status_code}")
        print(f"   Message:{response.message}")

        #6、再次读取
        print("再词读取上下文")
        response = client.read_context(context_id)
        print(f"   Status: {response.status_code}")
        print(f"   Message:{response.message}")



if __name__ == "__main__":
    example_basic_operation()